package com.wenda.homeschooedu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wenda.homeschooedu.biz.HomeworkManager;

import com.google.gson.Gson;

public class HomeworkServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out = null;
		String result = "" ;
		if (req.getParameter("format")!=null) {
			if (req.getParameter("format").equals("json")) {
				if (req.getParameter("type").equals("get")) {
					resp.setContentType("text/html;charset=utf-8");
					out = resp.getWriter();
					Gson gson = new Gson();
					
					int student_id =Integer.parseInt(req.getParameter("student_id"));
					int old_homework_id = Integer.parseInt(req.getParameter("old_homework_id"));
					
					result = gson.toJson(new HomeworkManager().getNewHomeWorkByStudentId(student_id, old_homework_id));
					out.print(result);
					out.flush();
					out.close();
				}
			}
		}
		
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
}
